code c cho DS1307
ds1307
code led trai tim
code dong ho matrix
Matran 8x64
dùng 74HC595 để quét cột,74373 để đệm hàng
; chung trinh quet led matrix 3led 8x8
;su dung 74hc595 de quet cot
;p1 du lieu hang
$include(reg51.inc)hang equ p1
dulieu bit p2.0
clk bit p2.1
org 0000h
clr dulieu
clr clk
main:
mov dptr,#maacall taoxung
b1: mov r2,#100 ;so chu ky lap lai hay toc do quet
b4: mov r3,#00 ;bien dem so cot
mov r1,#00 ;bien dem data
b3: clr a
setb clk
;acall tre
clr clk
mov a,r1
movc a,@a+dptr
mov hang ,a
acall delay
inc r3
inc r1
cjne r3,#48,b3
acall taoxung
djnz r2,b4
inc dptr
mov a,dpl
cjne a,#80,b1
sjmp main
delay:mov r5,#1
k1:mov r6,#20
djnz r6,$
djnz r5,k1
ret
taoxung:setb dulieu
setb clk
clr clk
clr dulieu
ret
tre: mov r7,#10
djnz r7,$
ret
org 0500h
ma: DB 0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH, 0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH, 0FFH,0FFH,0FFH,0FFH
DB 0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH, 0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH, 0FFH,0FFH,0FFH,0FFH
DB 0FFH,81H,0DFH,0EFH,0DFH,81H,0FFH,0FFH
DB 0FFH,81H,0ADH,0ADH,0ADH,0FFH,0FFH,0FFH
DB 0FFH,0C3H,0BDH,0BDH,0BDH,0FFH,0FFH,0FFH
DB 0FFH,0C3H,0BDH,0BDH,0BDH,0C3H,0FFH,0FFH
DB 0FFH,81H,0FBH,0F7H,0FBH,81H,0FFH,0FFH
DB 0FFH,81H,0ADH,0ADH,0ADH,0FFH,0FFH,0FFH
DB 0FFH,81H,0BDH,0BDH,0C3H,0FFH,0FFH,0FFH,0F3H,0EDH,0 DDH,0BBH,0DDH,0EDH,0F1H,0FFH,0FFH,0FFH,81H,0F7H,0F 7H,0D7H,81H,0FFH
DB 0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH, 0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH, 0FFH,0FFH,0FFH,0FFH
END
ok dồi ,và ic chốt 74595 thì chân chốt nối lên vcc la dc
Ma trân 16x32 chay tiên lui
#include <REGX52.H>
#include <string.h>
#include <math.h>
//******************BANG MA ASCII on LED matrix font 5x7 ****************
unsigned char code font[97][6]={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,//SPACE 0
0xFF,0xFF,0xA0,0xFF,0xFF,0xFF,//! 1
0xFF,0xFF,0xF8,0xF4,0xFF,0xFF,//' 2
0xEB,0x80,0xEB,0x80,0xEB,0xFF,//# 3
0xDB,0xD5,0x80,0xD5,0xED,0xFF,//$ 4
0xD8,0xEA,0x94,0xAB,0x8D,0xFF,//% 5
0xC9,0xB6,0xA9,0xDF,0xAF,0xFF,//& 6
0xFF,0xFF,0xF8,0xF4,0xFF,0xFF,//' 7
0xFF,0xE3,0xDD,0xBE,0xFF,0xFF,//( 8
0xFF,0xBE,0xDD,0xE3,0xFF,0xFF,//) 9
0xD5,0xE3,0x80,0xE3,0xD5,0xFF,//* 10
0xF7,0xF7,0xC1,0xF7,0xF7,0xFF,// 11
0xFF,0xA7,0xC7,0xFF,0xFF,0xFF,//, 12
0xF7,0xF7,0xF7,0xF7,0xF7,0xFF,//- 13
0xFF,0x9F,0x9F,0xFF,0xFF,0xFF,//x 14
0xFF,0xC9,0xC9,0xFF,0xFF,0xFF,// / 15
0xC1,0xAE,0xB6,0xBA,0xC1,0xFF,//0 16
0xFF,0xBD,0x80,0xBF,0xFF,0xFF,//1 17
0x8D,0xB6,0xB6,0xB6,0xB9,0xFF,//2 18
0xDD,0xBE,0xB6,0xB6,0xC9,0xFF,//3 19
0xE7,0xEB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0xC7,0xFF,//o 79
0x83,0xEB,0xEB,0xEB,0xF7,0xFF,//p 80
0xF7,0xEB,0xEB,0xEB,0x83,0xFF,//q 81
0x83,0xF7,0xFB,0xFB,0xF7,0xFF,//r 82
0xB7,0xAB,0xAB,0xAB,0xDB,0xFF,//s 83
0xFF,0xFB,0xC0,0xBB,0xBB,0xFF,//t 84
0xC3,0xBF,0xBF,0xDF,0x83,0xFF,//u 85
0xE3,0xDF,0xBF,0xDF,0xE3,0xFF,//v 86
0xC3,0xBF,0xCF,0xBF,0xC3,0xFF,//w 87
0xBB,0xD7,0xEF,0xD7,0xBB,0xFF,//x 88
0xF3,0xAF,0xAF,0xAF,0xC3,0xFF,//y 89
0xBB,0x9B,0xAB,0xB3,0xBB,0xFF,//z 90
0xFB,0xE1,0xE0,0xE1,0xFB,0xFF,//^ 62
0xE3,0xE3,0xC1,0xE3,0xF7,0xFF,//-> 93
0xF7,0xE3,0xC1,0xE3,0xE3,0xFF,//<- 94
0xEF,0xC3,0x83,0xC3,0xEF,0xFF,// 95
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,//BLANK CHAR 96
};
sfr DataPort1 = 0x90;
sfr Dataport3= 0xB0;
sbit sdi = P2^0;
sbit str = P2^1;
sbit clk = P2^2;
#define Tien 1
#define Lui 0
//-------------------------------------------
//-------------------------------------------
unsigned char Temp_code,a[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00};
unsigned int Pos=0,do_dai_chuoi,j,do_rong;
unsigned char t,i;
#define With_of_font 6
void wait(unsigned int n){ while(n--); }
void chaychu(unsigned char che_do,unsigned char *f)
{
int bat_dau,ket_thuc,do_dai_chuoi;
do_dai_chuoi=strlen(f)*6;
if(che_do==Tien){ bat_dau=0;ket_thuc= do_dai_chuoi-31; }
if(che_do==Lui ){ bat_dau=-do_dai_chuoi-32; ket_thuc= -31; }
do
{
for(t=0;t<10;t )
{
sdi=1;
for(i=0;i<32;i )
{
clk=1;clk=0;
str=1;str=0;
do_rong=abs(bat_dau i);
if(che_do==Tien) {do_rong=bat_dau i;}
if(che_do==Lui) {do_rong=abs(bat_dau-i);}
Temp_code=font[f[(do_rong%do_dai_chuoi)/6] -32][do_rong%6];
P1=Temp_code;
wait(60);
sdi=0;
}
}
bat_dau ;
} while(bat_dau!=ket_thuc);
P1=0xff;P3=0xff;wait(8000);
}
void main ()
{
while(1)
{
chaychu(Tien,"NGUYEN VAN BINH DT6-K5");
chaychu(Lui,"NGUYEN THUY NGA KINH TE K12");
}
}
Led ma trận 8x32
;..............CHƯƠNG TRÌNH CHÍNH.................
#include <REGX52.H>
#include <string.h>
//******************BANG MA ASCII on LED matrix font 5x7 ****************
unsigned char code code_Tab[97][6]={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,//SPACE 0
0xFF,0xFF,0xA0,0xFF,0xFF,0xFF,//! 1
0xFF,0xFF,0xF8,0xF4,0xFF,0xFF,//' 2
0xEB,0x80,0xEB,0x80,0xEB,0xFF,//# 3
0xDB,0xD5,0x80,0xD5,0xED,0xFF,//$ 4
0xD8,0xEA,0x94,0xAB,0x8D,0xFF,//% 5
0xC9,0xB6,0xA9,0xDF,0xAF,0xFF,//& 6
0xFF,0xFF,0xF8,0xF4,0xFF,0xFF,//' 7
0xFF,0xE3,0xDD,0xBE,0xFF,0xFF,//( 8
0xFF,0xBE,0xDD,0xE3,0xFF,0xFF,//) 9
0xD5,0xE3,0x80,0xE3,0xD5,0xFF,//* 10
0xF7,0xF7,0xC1,0xF7,0xF7,0xFF,//+ 11
0xFF,0xA7,0xC7,0xFF,0xFF,0xFF,//, 12
0xF7,0xF7,0xF7,0xF7,0xF7,0xFF,//- 13
0xFF,0x9F,0x9F,0xFF,0xFF,0xFF,//x 14
0xFF,0xC9,0xC9,0xFF,0xFF,0xFF,// / 15
0xC1,0xAE,0xB6,0xBA,0xC1,0xFF,//0 16
0xFF,0xBD,0x80,0xBF,0xFF,0xFF,//1 17
0x8D,0xB6,0xB6,0xB6,0xB9,0xFF,//2 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7,0xFB,0xFB,0x87,0xFF,//n 78
0xC7,0xBB,0xBB,0xBB,0xC7,0xFF,//o 79
0x83,0xEB,0xEB,0xEB,0xF7,0xFF,//p 80
0xF7,0xEB,0xEB,0xEB,0x83,0xFF,//q 81
0x83,0xF7,0xFB,0xFB,0xF7,0xFF,//r 82
0xB7,0xAB,0xAB,0xAB,0xDB,0xFF,//s 83
0xFF,0xFB,0xC0,0xBB,0xBB,0xFF,//t 84
0xC3,0xBF,0xBF,0xDF,0x83,0xFF,//u 85
0xE3,0xDF,0xBF,0xDF,0xE3,0xFF,//v 86
0xC3,0xBF,0xCF,0xBF,0xC3,0xFF,//w 87
0xBB,0xD7,0xEF,0xD7,0xBB,0xFF,//x 88
0xF3,0xAF,0xAF,0xAF,0xC3,0xFF,//y 89
0xBB,0x9B,0xAB,0xB3,0xBB,0xFF,//z 90
0xFB,0xE1,0xE0,0xE1,0xFB,0xFF,//^ 62
0xE3,0xE3,0xC1,0xE3,0xF7,0xFF,//-> 93
0xF7,0xE3,0xC1,0xE3,0xE3,0xFF,//<- 94
0xEF,0xC3,0x83,0xC3,0xEF,0xFF,// 95
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,//BLANK CHAR 96
};
sbit SDA = P2^0;
sbit SCL = P2^1;
sbit STR = P2^2;
sfr DataPort = 0x90;
//-------------------------------------------
//-------------------------------------------
unsigned char *String_input="Chao mung cac ban den voi website daihocdientu4.jimdo.com- Chuc cac ban co nhung giay phut vui ve tren website";
// thay doi chu chay bat ky o day !!
unsigned int Pos=0,lengh_STR;
unsigned char Tic=0;
#define With_of_font 6
void main (){
unsigned char Temp_code;
unsigned int j;
TMOD=0x01;
TH0=TL0=-100;
TR0=1;
ET0=1;
lengh_STR = strlen(String_input)*6;
EA=1;
while(1){
SDA=1;
for(j=Pos;j<32+Pos;j++){
Temp_code=code_Tab[ String_input[(j%lengh_STR)/With_of_font] -32][j%With_of_font];
DataPort=0xFF;
SCL=1;
SCL=0;
STR=1;
STR=0;
DataPort = Temp_code;
SDA=0;
}
}
}
void INT_Timer0()interrupt 1{
Tic++;
if(Tic==2){
Tic=0;
Pos++;
if(Pos>=lengh_STR)
Pos=0;
} }
Led sao băng 1
#include<regx51.h>
#define NumPort 32
char PWM[NumPort];
char chuki=0;
/*------------------------------------------------------------------------*/
void SetPort(char i, char v)
{
switch (i)
{
case 0: P1_0 = v; return;
case 1: P1_1 = v; return;
case 2: P1_2 = v; return;
case 3: P1_3 = v; return;
case 4: P1_4 = v; return;
case 5: P1_5 = v; return;
case 6: P1_6 = v; return;
case 7: P1_7 = v; return;
case 8: P3_0 = v; return;
case 9: P3_1 = v; return;
case 10: P3_2 = v; return;
case 11: P3_3 = v; return;
case 12: P3_4 = v; return;
case 13: P3_5 = v; return;
case 14: P3_6 = v; return;
case 15: P3_7 = v; return;
case 16: P2_0 = v; return;
case 17: P2_1 = v; return;
case 18: P2_2 = v; return;
case 19: P2_3 = v; return;
case 20: P2_4 = v; return;
case 21: P2_5 = v; return;
case 22: P2_6 = v; return;
case 23: P2_7 = v; return;
case 24: P0_7 = v; return;
case 25: P0_6 = v; return;
case 26: P0_5 = v; return;
case 27: P0_4 = v; return;
case 28: P0_3 = v; return;
case 29: P0_2 = v; return;
case 30: P0_1 = v; return;
case 31: P0_0 = v; return;
}
}
/*------------------------------------------------------------------------*/
void Timer0()interrupt 1
{
char i;
TF0=0;
TR0=0; //stop ngat
for (i=0 ; i<NumPort ; i++)
if (chuki < PWM[i]) SetPort(i, 1);
else SetPort(i, 0);
chuki += 5; //cho tu 5 den 10 se it ca dut, tuy hy nhe
if (chuki >= 100) chuki=0;
TR0=1; //tiep tuc ngat
}
void delay(unsigned int ms)
{
for (ms=ms*120; ms>0 ; ms--);
}
/*------------------------------------------------------------------------*/
void ShiftRightArray(char Arr[], char v1, char v2) // 0--------------->n
{
char i, temp=Arr[v2];
for (i=v2 ; i>v1 ; i--) Arr[i] = Arr[i-1];
Arr[v1] = temp;
}
void ShiftLeftArray(char Arr[], char v1, char v2) // 0<---------------n
{
char i, temp=Arr[v1];
for (i=v1 ; i < v2 ; i++) Arr[i] = Arr[i+1];
Arr[v2] = temp;
}
char MinArray(char Arr[], char v1, char v2)
{
char i, vMin=v1;
for (i=v1+1 ; i <= v2 ; i++)
if (Arr[i] < Arr[vMin]) vMin=i;
return vMin;
}
char MaxArray(char Arr[], char v1, char v2)
{
char i, vMax=v1;
for (i=v1+1 ; i <= v2 ; i++)
if (Arr[i] > Arr[vMax]) vMax=i;
return vMax;
}
void SetArray(char Arr[], char v1, char v2, unsigned char val)
{
char i;
for (i=v1 ; i <= v2 ; i++) Arr[i]=val;
}
/*---------------Hieu ung vien sao bang gom 8 cang cuc dep nhe---------------------------------*/
char IdBorder=0;
unsigned int StepBorder=0;
void Border()
{
if (StepBorder == 0)
{
PWM[0]=10; StepBorder++;
SetArray(PWM, 1, 7, 0);
}
else if (StepBorder < 224)
{
if (StepBorder%32 ==0)
{
IdBorder = MaxArray(PWM, 0, 7);
if (PWM[IdBorder] == 100)
{
PWM[0]=10;
SetArray(PWM, 1, 7, 0);
}
else PWM[(IdBorder+1)%8] = PWM[IdBorder] + 15;
}
StepBorder++;
ShiftRightArray(PWM, 0, 7);
}
else if (StepBorder == 224)
{
PWM[0]=10; StepBorder++;
SetArray(PWM, 1, 7, 0);
}
else if (StepBorder < 448)
{
if (StepBorder%32 ==0)
{
IdBorder = MaxArray(PWM, 0, 7);
if (PWM[IdBorder] == 100)
{
PWM[0]=10;
SetArray(PWM, 1,7,0);
}
else if (IdBorder == 0) PWM[7] = PWM[0] + 15;
else PWM[IdBorder-1] = PWM[IdBorder] + 15;
}
StepBorder++;
ShiftLeftArray(PWM, 0, 7);
}
else if (StepBorder == 448)
{
SetArray(PWM, 0, 7, 0);
IdBorder = 0; StepBorder++;
}
else if (StepBorder < 460)
{
if (StepBorder%2 == 0)
{
IdBorder =(IdBorder)?0:100;
SetArray(PWM, 0, 7, IdBorder);
}
StepBorder++;
}
else StepBorder=0;
}
/*------------------------------------------------------------------------
PWM[0] ...PWM[31] ung voi 32cang 89c52
moi 1 bien PWM[i] co gia tri tu 0 -> 100 ung voi do sang
---------------------------------------------------------------------------*/
main()
{
EA=0;
TMOD=0x02; //timer 0: che do 2: 8 bit tu dong nap lai
TH0 = 156;
TL0 = 156;
ET0=1;
EA=1;
TR0=1;
while (1)
{
TF0=0;
TR0=0; //stop ngat
Border();
TR0=1; //tiep tuc ngat
delay(5);
}
}
/**************************Chu thot thank cai di ************************************/
Led sao băng 2
#include <REGX51.H>
unsigned char led[16]={255,250,150,90,30,15,12,8,6,4,4,1,1,0,0,0};
void delay_ms(unsigned int ms,ttt){
//65535ms 9000MHz
while(ms--){
unsigned int xxx = 7*ttt+1;
while(xxx--);
}
}
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
/*void delay(unsigned long t)
{
unsigned int m;
for(m=0;m<t;m++);
}*/
unsigned char dem=0,nho;
/*xxxxxxxxxxxxxxx*/
void Timer0()interrupt 1 {
TF0=0; TR0=0;
//if(dem==0) {P0=0;P2=0;}
if(dem>=led[0]) P0_0=0; else P0_0=1;
if(dem>=led[1]) P0_1=0; else P0_1=1;
if(dem>=led[2]) P0_2=0; else P0_2=1;
if(dem>=led[3]) P0_3=0; else P0_3=1;
if(dem>=led[4]) P0_4=0; else P0_4=1;
if(dem>=led[5]) P0_5=0; else P0_5=1;
if(dem>=led[6]) P0_6=0; else P0_6=1;
if(dem>=led[7]) P0_7=0; else P0_7=1;
if(dem>=led[8]) P2_7=0; else P2_7=1;
if(dem>=led[9]) P2_6=0; else P2_6=1;
if(dem>=led[10]) P2_5=0; elseP2_5=1;
if(dem>=led[11]) P2_4=0; elseP2_4=1;
if(dem>=led[12]) P2_3=0; elseP2_3=1;
if(dem>=led[13]) P2_2=0; elseP2_2=1;
if(dem>=led[14]) P2_1=0; elseP2_1=1;
if(dem>=led[15]) P2_0=0; elseP2_0=1;
dem++;
TR0=1;
}
/*xxxxxxxxxxxxxxx*/
/*void Timer1()interrupt 3 {
TF1=0; TR1=0;
//if(dem==0) {P0=0;P2=0;}
if(dem>=led[15]) P0_0=0; elseP0_0=1;
if(dem>=led[14]) P0_1=0; elseP0_1=1;
if(dem>=led[13]) P0_2=0; elseP0_2=1;
if(dem>=led[12]) P0_3=0; elseP0_3=1;
if(dem>=led[11]) P0_4=0; elseP0_4=1;
if(dem>=led[10]) P0_5=0; elseP0_5=1;
if(dem>=led[9]) P0_6=0; else P0_6=1;
if(dem>=led[8]) P0_7=0; else P0_7=1;
if(dem>=led[7]) P2_7=0; else P2_7=1;
if(dem>=led[6]) P2_6=0; else P2_6=1;
if(dem>=led[5]) P2_5=0; else P2_5=1;
if(dem>=led[4]) P2_4=0; else P2_4=1;
if(dem>=led[3]) P2_3=0; else P2_3=1;
if(dem>=led[2]) P2_2=0; else P2_2=1;
if(dem>=led[1]) P2_1=0; else P2_1=1;
if(dem>=led[0]) P2_0=0; else P2_0=1;
dem++;
TR1=1;
}*/
/*xxxxxxxxxxxxxxxxxxxxx quay phai xxxxxxxxxxxxxxxxxxxxx*/
void main(void)
{
while(1)
{
unsigned int i,j,k;
TH0=180;
TL0=180;
TMOD=0x02; //timer 0: che do 2: 8 bit tu dong nap lai
EA=1; ET0=1; TR0=1;
for(i=1;i<500;i++)
{
delay_ms(15,10); dem=0;
nho= led[0];
led[0]= led[1]; led[1]= led[2]; led[2]= led[3]; led[3]= led[4];
led[4]= led[5]; led[5]= led[6]; led[6]= led[7]; led[7]= led[8];
led[8]= led[9]; led[9]= led[10]; led[10]=led[11]; led[11]=led[12];
led[12]=led[13]; led[13]=led[14]; led[14]=led[15]; led[15]=nho;
}
/*xxxxxxxxxxxxxx quay phai xxxxxxxxxxxxxxxxxxx*/
/*TR0=0;
TH1=180;
TL1=180;
TMOD=0x02; //timer 0: che do 2: 8 bit tu dong nap lai
EA=1; ET1=1; TR1=1;*/
for(j=1;j<500;j++)
{
delay_ms(15,10); dem=0;
nho= led[15];
led[15]= led[14]; led[14]= led[13]; led[13]= led[12]; led[12]= led[11];
led[11]= led[10]; led[10]= led[9]; led[9]= led[8]; led[8]= led[7];
led[7]= led[6]; led[6]= led[5]; led[5]=led[4]; led[4]=led[3];
led[3]=led[2]; led[2]=led[1]; led[1]=led[0]; led[0]=nho;
}
/*xxxxxxx chop tat xxxxxxxxxxxxxxxxxxxxxxx*/
TR0=0;
for(k=1;k<20;k++)
{
P0_0=0;
P0_1=0;
P0_2=0;
P0_3=0;
P0_4=0;
P0_5=0;
P0_6=0;
P0_7=0;
P2_7=0;
P2_6=0;
P2_5=0;
P2_4=0;
P2_3=0;
P2_2=0;
P2_1=0;
P2_0=0;
delay_ms(60,30);
P0_0=1;
P0_1=1;
P0_2=1;
P0_3=1;
P0_4=1;
P0_5=1;
P0_6=1;
P0_7=1;
P2_7=1;
P2_6=1;
P2_5=1;
P2_4=1;
P2_3=1;
P2_2=1;
P2_1=1;
P2_0=1;
delay_ms(60,30);
}
}}
Sao băng 3
//Ngo Hung Cuong//kidteam@gmail.com//0989696971 - 0915696971#include <regx51.h>
unsigned int code mang_t0[]={0,2000,4000,6000,8000,10000,12000,14000,16000,18000,20000};
unsigned int code mang_t1[]={20000,18000,16000,14000,12000,10000,8000,6000,4000,2000,0};
unsigned char count_s;unsigned char count;void T0_ISR();void T1_ISR();void init();
void main() {
init();
while(1);}
void init() {
count=0;//10 cap do count_s=0; TMOD=0x11;//timer 0, 1 che do 1 16 bit P1=0xff; TR0=1; TF0=1;//ngat luon EA=1; ET0=1; ET1=1;}
void T0_ISR() interrupt TF0_VECTOR { TR0=0; TH1=(0xffff-mang_t1[count])>>8; TL1=(0xffff-mang_t1[count])&0xff; //P1=mp[count]; P1=0xff; TR1=1;}
void T1_ISR() interrupt TF1_VECTOR { TR1=0; TH0=(0xffff-mang_t0[count])>>8; TL0=(0xffff-mang_t0[count])&0xff; ++count_s; if(count_s==5) { count_s=0; ++count; if(count==11) { count=0; } } P1=0x00; TR0=1;}
Sao băng 4
pwm_1 bit p3.0 pwm_2 bit p3.1 pwm_3 bit p3.2 pwm_4 bit p3.3 org 00h ljmp main org 0bh ljmp timer0 org 40hmain: mov sp,#5fh mov tmod,#02h ;che do auto reload 8bit mov ie,#82h ;cho phep cac ngat và timer0 hoat dong mov th0,#-1 mov tl0,#-1 setb tr0 mov dem,#0 mov duty_1,#0 mov duty_2,#0 mov duty_3,#0 mov duty_4,#0sjmp $timer0: push acc ;cat du lieu vao ngan xep push psw inc dem mov a,dem cjne a,duty_1,clr_pwm_2 clr pwm_1clr_pwm_2: cjne a,duty_2,clr_pwm_3 clr pwm_2clr_pwm_3: cjne a,duty_3,clr_pwm_4 clr pwm_3clr_pwm_4: cjne a,duty_4,setb_pwm clr pwm_4setb_pwm: cjne a,#255,exit setb p3.0 setb p3.1 setb p3.2 setb p3.3exit: pop psw ;lay du lieu tu ngan xep pop acc retiend các duty để điều chỉnh độ rộng xung nhá (0<=duty_x<=255), bạn dựa vào đó mà phát triển lên nhiều PWM hơn
Sao băng 5
/*Thach anh su dung f=24Mhz*32 cong/#include "regx52.h"typedef unsigned char uchar;typedef unsigned int uint;sbit c1=P0^0;sbit c2=P0^1;sbit c3=P0^2;sbit c4=P0^3;sbit c5=P0^4;sbit c6=P0^5;sbit c7=P0^6;sbit c8=P0^7;sbit c9=P2^7;sbit c10=P2^6;sbit c11=P2^5;sbit c12=P2^4;sbit c13=P2^3;sbit c14=P2^2;sbit c15=P2^1;sbit c16=P2^0;sbit c17=P3^7;sbit c18=P3^6;sbit c19=P3^5;sbit c20=P3^4;sbit c21=P3^3;sbit c22=P3^2;sbit c23=P3^1;sbit c24=P3^0;sbit c25=P1^7;sbit c26=P1^6;sbit c27=P1^5;sbit c28=P1^4;sbit c29=P1^3;sbit c30=P1^2;sbit c31=P1^1;sbit c32=P1^0; uchar num_pwm;uchar pwm_c[32];uint delay_in_t1;void isr_timer1() interrupt 3 using 2{TH1=0xFE; /* -24000000/12/60/100 ~= -333 ->0xfeb3(100 nghia la 100 muc sang)*/TL1=0xB3; /* chu ky ngat timer1 T=(333/2)us --> T*6 = 999 us ~= 1ms*/ c1=(pwm_c[0]>num_pwm)?1:0;c2=(pwm_c[1]>num_pwm)?1:0;c3=(pwm_c[2]>num_pwm)?1:0;c4=(pwm_c[3]>num_pwm)?1:0;c5=(pwm_c[4]>num_pwm)?1:0; c6=(pwm_c[5]>num_pwm)?1:0;c7=(pwm_c[6]>num_pwm)?1:0;c8=(pwm_c[7]>num_pwm)?1:0;c9=(pwm_c[8]>num_pwm)?1:0;c10=(pwm_c[9]>num_pwm)?1:0;c11=(pwm_c[10]>num_pwm)?1:0;c12=(pwm_c[11]>num_pwm)?1:0;c13=(pwm_c[12]>num_pwm)?1:0;c14=(pwm_c[13]>num_pwm)?1:0;c15=(pwm_c[14]>num_pwm)?1:0;c16=(pwm_c[15]>num_pwm)?1:0;c17=(pwm_c[16]>num_pwm)?1:0;c18=(pwm_c[17]>num_pwm)?1:0;c19=(pwm_c[18]>num_pwm)?1:0;c20=(pwm_c[19]>num_pwm)?1:0;c21=(pwm_c[20]>num_pwm)?1:0;c22=(pwm_c[21]>num_pwm)?1:0;c23=(pwm_c[22]>num_pwm)?1:0;c24=(pwm_c[23]>num_pwm)?1:0;c25=(pwm_c[24]>num_pwm)?1:0;c26=(pwm_c[25]>num_pwm)?1:0;c27=(pwm_c[26]>num_pwm)?1:0;c28=(pwm_c[27]>num_pwm)?1:0;c29=(pwm_c[28]>num_pwm)?1:0;c30=(pwm_c[29]>num_pwm)?1:0;c31=(pwm_c[30]>num_pwm)?1:0;c32=(pwm_c[31]>num_pwm)?1:0;if(++num_pwm==100){ num_pwm=0;}++delay_in_t1; }void delay_ms(uint time){ delay_in_t1 = 0;while(delay_in_t1 <= time*6);}void main(){TMOD=0x10; /*timer 1 16bit*/TR1=1;TH1=0xFE;TL1=0xB3;IE=0x88; /*Ket thuc khoi dong timer1*/P0 = 0;P1 = 0;P2 = 0;P3 = 0;delay_ms(3000); while(1){}}
Sao băng 6
/*Thach anh su dung f=24Mhz*/#include "regx52.h"typedef unsigned char uchar;typedef unsigned int uint;sbit c1=P0^0;sbit c2=P0^1;sbit c3=P0^2;sbit c4=P0^3;sbit c5=P0^4;sbit c6=P0^5;sbit c7=P0^6;sbit c8=P0^7;sbit c9=P2^7;sbit c10=P2^6;sbit c11=P2^5;sbit c12=P2^4;sbit c13=P2^3;sbit c14=P2^2;sbit c15=P2^1;sbit c16=P2^0;sbit c17=P3^7;sbit c18=P3^6;sbit c19=P3^5;sbit c20=P3^4;sbit c21=P3^3;sbit c22=P3^2;sbit c23=P3^1;sbit c24=P3^0;sbit c25=P1^7;sbit c26=P1^6;sbit c27=P1^5;sbit c28=P1^4;sbit c29=P1^3;sbit c30=P1^2;sbit c31=P1^1;sbit c32=P1^0; uchar num_pwm;uchar pwm_c[32];uint delay_in_t1;void isr_timer1() interrupt 3 using 2{TH1=0xFD; /* -24000000/12/60/50 ~= -666 ->0xFD66(50 nghia la 50 muc sang)*/TL1=0x66; /* chu ky ngat timer1 T=(666/2)us --> T*3 = 999 us ~= 1ms*/ c1=(pwm_c[0]>num_pwm)?1:0;c2=(pwm_c[1]>num_pwm)?1:0;c3=(pwm_c[2]>num_pwm)?1:0;c4=(pwm_c[3]>num_pwm)?1:0;c5=(pwm_c[4]>num_pwm)?1:0; c6=(pwm_c[5]>num_pwm)?1:0;c7=(pwm_c[6]>num_pwm)?1:0;c8=(pwm_c[7]>num_pwm)?1:0;c9=(pwm_c[8]>num_pwm)?1:0;c10=(pwm_c[9]>num_pwm)?1:0;c11=(pwm_c[10]>num_pwm)?1:0;c12=(pwm_c[11]>num_pwm)?1:0;c13=(pwm_c[12]>num_pwm)?1:0;c14=(pwm_c[13]>num_pwm)?1:0;c15=(pwm_c[14]>num_pwm)?1:0;c16=(pwm_c[15]>num_pwm)?1:0;c17=(pwm_c[16]>num_pwm)?1:0;c18=(pwm_c[17]>num_pwm)?1:0;c19=(pwm_c[18]>num_pwm)?1:0;c20=(pwm_c[19]>num_pwm)?1:0;c21=(pwm_c[20]>num_pwm)?1:0;c22=(pwm_c[21]>num_pwm)?1:0;c23=(pwm_c[22]>num_pwm)?1:0;c24=(pwm_c[23]>num_pwm)?1:0;c25=(pwm_c[24]>num_pwm)?1:0;c26=(pwm_c[25]>num_pwm)?1:0;c27=(pwm_c[26]>num_pwm)?1:0;c28=(pwm_c[27]>num_pwm)?1:0;c29=(pwm_c[28]>num_pwm)?1:0;c30=(pwm_c[29]>num_pwm)?1:0;c31=(pwm_c[30]>num_pwm)?1:0;c32=(pwm_c[31]>num_pwm)?1:0;if(++num_pwm==100){ num_pwm=0;}++delay_in_t1; }void delay_ms(uint time){ delay_in_t1 = 0;while(delay_in_t1 <= time*3);}void main(){TMOD=0x10; /*timer 1 16bit*/TR1=1;TH1=0xFD;TL1=0x66;IE=0x88; /*Ket thuc khoi dong timer1*/P0 = 0;P1 = 0;P2 = 0;P3 = 0;delay_ms(3000); while(1){}}